package com.earl.gsm.web.place;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.earl.gsm.commons.vo.DwzMessage;
import com.earl.gsm.domain.gsm.place.PlaceOrder;
import com.earl.gsm.domain.sercurity.user.User;
import com.earl.gsm.service.PlaceOrderService;
import com.earl.gsm.web.BaseController;
import com.wordnik.swagger.annotations.ApiOperation;

@Controller
@RequestMapping(value = "/placeorder")
//@Api(value="Person Rest Service") //修改生成的son路径
public class PlaceOrderController extends BaseController {

    private final Logger log = LoggerFactory.getLogger(PlaceOrderController.class);

    @Autowired
    private PlaceOrderService service;

    /**
     * GET /users -> get all the users
     */
    @RequestMapping(value = "/allUnOutOfDate")//查询所有未过期的订单
    @ApiOperation(value = "得到所有的申请信息", notes = "find All User",  response = User.class, responseContainer = "List")
    public ModelAndView getAllUnOutOfDate() {
        log.debug("page show all apply for create match");
        
        //查询前先更新数据库过期数据，在查询
        service.updateOutOfDate();
        
        PlaceOrder tmporder = new PlaceOrder();
        tmporder.setOutOfDate(false);
        List<PlaceOrder> placeOrderList = service.searchQuery(tmporder);
//        List<PlaceOrder> placeOrderList = service.findAll();
        return new ModelAndView("place/allPlaceOrder","placeOrderList",placeOrderList);
    }
    
    /**
     * GET /users -> get all the users
     */
    @RequestMapping(value = "/allOutOfDate")//查询所有未过期的订单
    @ApiOperation(value = "得到所有的申请信息", notes = "find All User", response = User.class, responseContainer = "List")
    public ModelAndView getAllOutOfDate() {
        log.debug("page show all apply for create match");
        
        //查询前先更新数据库过期数据，在查询
        service.updateOutOfDate();
        
        PlaceOrder tmporder = new PlaceOrder();
        tmporder.setOutOfDate(true);
        List<PlaceOrder> placeOrderList = service.searchQuery(tmporder);
//        List<PlaceOrder> placeOrderList = service.findAll();
        return new ModelAndView("place/allPlaceOrder","placeOrderList",placeOrderList);
    }
    
    /**
     * GET /users -> get all the users
     */
    @RequestMapping(value = "/all")//查询所有未过期的订单
    @ApiOperation(value = "得到所有的申请信息", notes = "find All User", httpMethod = "GET", response = User.class, responseContainer = "List")
    public ModelAndView getAll() {
        log.debug("page show all apply for create match");
        
        //查询前先更新数据库过期数据，在查询
        service.updateOutOfDate();  
        List<PlaceOrder> placeOrderList = service.findAll();
        return new ModelAndView("place/allPlaceOrder","placeOrderList",placeOrderList);
    }

    /**
     * 服务端预处理添加事件
     * @param
     * @return
     */
    @RequestMapping(value="/preAdd")
    @ApiOperation(value = "添加场地订单", notes = "find All User", httpMethod = "GET", response = User.class, responseContainer = "List")
    public String preAdd(){
        return "place/addPlaceOrder";
    }  
    
    /**
     * 服务端添加场地订单
     * @param place
     * @return
     */
    @RequestMapping(value="/add")
    @ApiOperation(value = "添加场地订单", notes = "add new match", httpMethod = "POST", response = DwzMessage.class)
    public ResponseEntity<DwzMessage> applyForMatch(PlaceOrder placeOrder){

    	service.saveOrder(placeOrder);
        DwzMessage dwzMessage = new DwzMessage();
        dwzMessage.setStatusCode(200);
        dwzMessage.setMessage("添加成功");
        dwzMessage.setCallbackType("forward");
        dwzMessage.setForwardUrl("/placeorder/all");
        dwzMessage.setNavTabId("场地信息详情");
        return new ResponseEntity<DwzMessage>(dwzMessage,HttpStatus.OK);
    }



    /**
     * 用户完善场地信息
     * @return
     */
    @RequestMapping(value="/preUpdate/{id}")
    @ApiOperation(value = "预处理更新场地信息", notes = "find All User", httpMethod = "GET", response = User.class, responseContainer = "List")
    public ModelAndView preUpdate(@PathVariable  Long id){
        service.findOne(id);
        return new ModelAndView("match/preUpdate","match", service.findOne(id));
    }

    /**
     * 服务端或者用户端更新赛事
     * @return
     */
    @RequestMapping(value="/update")
    @ApiOperation(value = "更新场地", notes = "find All User", httpMethod = "GET", response = User.class, responseContainer = "List")
    public ResponseEntity<DwzMessage> update(PlaceOrder place){
    	service.updateByPrimaryKeySelective(place);
    	 DwzMessage dwzMessage = new DwzMessage();
    	 dwzMessage.setMessage("添加成功");
         dwzMessage.setCallbackType("forward");
         dwzMessage.setForwardUrl("/match/uncompelete");
         dwzMessage.setNavTabId("场地信息详情");
        return new ResponseEntity<DwzMessage>(dwzMessage,HttpStatus.OK);
    }

    
    /**
     * 服务端删除场地
     * @param id applyid
     * @return
     */
    @RequestMapping(value="/delete/{id}")
    @ApiOperation(value = "删除场地信息", notes = "find All User", httpMethod = "GET", response = User.class, responseContainer = "List")
    public ResponseEntity<DwzMessage> allMatchApply(@PathVariable Long id){
        service.delete(id);
        DwzMessage dwzMessage = new DwzMessage();
        dwzMessage.setMessage("删除成功");
        dwzMessage.setCallbackType("forward");
        dwzMessage.setForwardUrl("/placeorder/all");
        dwzMessage.setStatusCode(200);  //状态码200表示请求成功
        return new ResponseEntity<DwzMessage>(dwzMessage,HttpStatus.OK);
    }
}
